<template>
  <a-layout class="dashboard">
    <a-layout-sider
      v-model:collapsed="collapsed"
      :trigger="null"
      collapsible
      class="dashboard-sider"
    >
      <div class="logo">
        <h2>WatermarkRemover</h2>
      </div>
      
      <a-menu
        v-model:selectedKeys="selectedKeys"
        mode="inline"
        theme="dark"
        class="dashboard-menu"
      >
        <a-menu-item key="home">
          <router-link to="/dashboard">
            <DashboardOutlined />
            <span>控制台</span>
          </router-link>
        </a-menu-item>
        
        <a-menu-item key="upload">
          <router-link to="/dashboard/upload">
            <CloudUploadOutlined />
            <span>文件上传</span>
          </router-link>
        </a-menu-item>
        
        <a-menu-item key="history">
          <router-link to="/dashboard/history">
            <HistoryOutlined />
            <span>处理历史</span>
          </router-link>
        </a-menu-item>
        
        <a-menu-item key="profile">
          <router-link to="/dashboard/profile">
            <UserOutlined />
            <span>个人资料</span>
          </router-link>
        </a-menu-item>
        
        <a-menu-item key="settings">
          <router-link to="/dashboard/settings">
            <SettingOutlined />
            <span>设置</span>
          </router-link>
        </a-menu-item>
      </a-menu>
    </a-layout-sider>
    
    <a-layout>
      <a-layout-header class="dashboard-header">
        <div class="header-left">
          <a-button
            type="text"
            @click="collapsed = !collapsed"
            class="trigger-btn"
          >
            <MenuUnfoldOutlined v-if="collapsed" />
            <MenuFoldOutlined v-else />
          </a-button>
          <a-breadcrumb>
            <a-breadcrumb-item>控制台</a-breadcrumb-item>
            <a-breadcrumb-item>{{ currentPageTitle }}</a-breadcrumb-item>
          </a-breadcrumb>
        </div>
        
        <div class="header-right">
          <a-dropdown>
            <a-button type="text" class="user-dropdown">
              <a-avatar :src="authStore.user?.avatar" size="small">
                {{ authStore.user?.username?.charAt(0)?.toUpperCase() }}
              </a-avatar>
              <span class="username">{{ authStore.user?.username }}</span>
              <DownOutlined />
            </a-button>
            <template #overlay>
              <a-menu>
                <a-menu-item key="profile">
                  <router-link to="/dashboard/profile">
                    <UserOutlined />
                    个人资料
                  </router-link>
                </a-menu-item>
                <a-menu-item key="settings">
                  <router-link to="/dashboard/settings">
                    <SettingOutlined />
                    设置
                  </router-link>
                </a-menu-item>
                <a-menu-divider />
                <a-menu-item key="logout" @click="handleLogout">
                  <LogoutOutlined />
                  退出登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
      </a-layout-header>
      
      <a-layout-content class="dashboard-content">
        <router-view />
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import { message } from 'ant-design-vue'
import {
  DashboardOutlined,
  CloudUploadOutlined,
  HistoryOutlined,
  UserOutlined,
  SettingOutlined,
  MenuUnfoldOutlined,
  MenuFoldOutlined,
  DownOutlined,
  LogoutOutlined
} from '@ant-design/icons-vue'

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()

const collapsed = ref(false)
const selectedKeys = ref(['home'])

const currentPageTitle = computed(() => {
  const routeMap = {
    '/dashboard': '控制台',
    '/dashboard/upload': '文件上传',
    '/dashboard/history': '处理历史',
    '/dashboard/profile': '个人资料',
    '/dashboard/settings': '设置'
  }
  return routeMap[route.path] || '控制台'
})

const handleLogout = () => {
  authStore.logout()
  message.success('已退出登录')
  router.push('/login')
}

// 监听路由变化，更新选中的菜单项
watch(() => route.path, (newPath) => {
  const keyMap = {
    '/dashboard': 'home',
    '/dashboard/upload': 'upload',
    '/dashboard/history': 'history',
    '/dashboard/profile': 'profile',
    '/dashboard/settings': 'settings'
  }
  selectedKeys.value = [keyMap[newPath] || 'home']
}, { immediate: true })
</script>

<style scoped>
.dashboard {
  min-height: 100vh;
}

.dashboard-sider {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: 1000;
}

.logo {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #001529;
  color: white;
  font-size: 16px;
  font-weight: 600;
}

.logo h2 {
  margin: 0;
  color: #1890ff;
}

.dashboard-menu {
  border-right: none;
}

.dashboard-menu a {
  color: inherit;
  text-decoration: none;
}

.dashboard-header {
  background: white;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 999;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.trigger-btn {
  font-size: 16px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 40px;
  padding: 0 12px;
}

.username {
  margin-left: 8px;
  font-weight: 500;
}

.dashboard-content {
  margin: 24px;
  padding: 24px;
  background: white;
  border-radius: 8px;
  min-height: calc(100vh - 112px);
}

@media (max-width: 768px) {
  .dashboard-content {
    margin: 16px;
    padding: 16px;
  }
  
  .username {
    display: none;
  }
}
</style> 